#include "list.h"

/*
表的创建
创建一个Link_list的值作为头文件返回去
*/
Link_list *init_link_list(void)
{
    Link_list *L = (Link_list *)malloc(sizeof(Link_list));
    if (L == NULL)
    {
        printf("创建失败");
        return NULL;
    }
    printf("创建成功\n");
    return L;
}

/*
判空
传递头文件判断是否文空
*/
int IsEmpty_Sql_List(Link_list *list)
{
    if (list != NULL)
        return 1;
    return 0;
}
/*
遍历
输出链表里面所有的值
*/
void print_link_list(Node *list)
{
    if (list == NULL)
    {
        printf("没有值\n");
        return;
    }
    for (int i = 0; list != NULL; i++)
    {
        printf("姓名 %s\t 性别 %s\t 成绩 %d\t 学号 %d\t\n",
               list->data.name, list->data.sex,
               list->data.age, list->data.id);
        list = list->next;
    }
}

/*
插入
在中间插入
*/
void insert_node(Node *list, add data, int index)
{
    for (int i = 0; i < index; i++)
    {
        list = list->next;
    }
    Node *L1 = list->next;
    Node *L2 = (Node *)malloc(sizeof(Node));
    L2->data = data;
    L2->next = L1;
    list->next = L2;
}

/*
删除
删除中间的指针
*/
void delete_node(Node *list, int index)
{
    Node *L2 = (Node *)malloc(sizeof(Node));
    for (int i = 0; i <= index; i++)
    {
        L2 = list;
        list = list->next;
    }
    L2->next = list->next;
}
/*
尾插
*/
void Inser_node(Node *list, add data)
{
    for (int i = 0; list->next != NULL; i++)
    {
        list = list->next;
    }
    Node *L2 = (Node *)malloc(sizeof(Node));
    L2->data = data;
    list->next = L2;
    L2->next = NULL;
}
/*
尾删
*/
void Delete_node(Node *list)
{
    for (; list->next->next != NULL;)
        list = list->next;
    list->next = NULL;
}
/*
销毁
销毁链表
*/
void free_link_list(Node **list)
{
   for (int i = 0; *list != NULL; i++)
    {
        Node *L1 = (Node *)malloc(sizeof(Node));
        (*list) = L1;
        free(L1);
        *list = (*list)->next;
    }
   
    list = NULL;
}
/*
头插
插入第一个
*/
void head_Inser_node(Node **list, add data)
{
    Node *L2 = (Node *)malloc(sizeof(Node));
    L2->data = data;
    L2->next = (*list);
    (*list) = L2;
}
/*
头删
删除第一个
*/
void head_Delete_node(Node** list)
{
    *list=(*list)->next;
}
